// 点击右上角切换登录方式
var wx = document.querySelector(".wx");
var pc = document.querySelector(".pc");
var rightTop = document.querySelector(".rightTop");
rightTop.addEventListener("click", changeLogin);
function changeLogin() {
    var gs = getComputedStyle;
    if (gs(wx)["display"] === "block") {
        wx.style.display = "none";
        pc.style.display = "block";
    } else {
        wx.style.display = "block";
        pc.style.display = "none";
    }
}

// 表单验证
var phone_email = document.querySelector("#phone_email");
var pass = document.querySelector("#pass");
var usrError = document.querySelector(".usrError");
var passError = document.querySelector(".passError");
var loginBtn = document.querySelector("#loginBtn");
var validArr = [phone_email, pass];
phone_email.addEventListener("focus", inputFocus(phone_email));
phone_email.addEventListener("blur", inputBlur(phone_email));
pass.addEventListener("focus", inputFocus(pass));
pass.addEventListener("blur", inputBlur(pass));
loginBtn.addEventListener("click", loginSubmit);
function inputFocus(dom) {
    return function () {
        dom.style.background = "#ffffff";
    }
}
function inputBlur(dom) {
    return function () {
        dom.style.background = "#f4f4f4";
        validate(dom);
    }
}
function validate(dom) {
    if (dom.id === "phone_email") {
        validUsr();
    } else {
        validPass();
    }
}
function success(input, tipEle) {
    // alert("成功")
    input.classList.remove("success");
    tipEle.style.display = "none";
    input.className += " success";

}

function error(tip, input, tipEle) {
    // alert("失败")
    // 还原
    tipEle.style.display = "block";
    input.classList.remove("success");
    // 报错;
    // tipEle.className = "warn";
    tipEle.innerHTML = tip;
}
function validUsr() {
    var usrValue = phone_email.value;
    var regPhone = /1[3-9]\d{9}/;
    var regEmail = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@[0-9a-z]{2,9}\.[a-z]{2,6}(\.[a-z]{2,3})?$/i;
    if (regPhone.test(usrValue) || regEmail.test(usrValue)) {
        // usrError.style.display = "none";
        // phone_email.className += " success";
        success(phone_email, usrError);
    } else {
        // usrError.style.display = "block";
        // phone_email.classList.remove("success");
        error("请输入正确的11位手机号或者邮箱", phone_email, usrError);
    }
}
function validPass() {
    var passValue = pass.value;
    var strength = 0;
    var regList = {
        "hasNumber": /\d/,
        "hasLetter": /[a-zA-Z]/,
        "hasSC": /[\!\@\#\$\%\^]/,
        "minlength6": /^.{0,6}$/
    }
    if (regList.hasNumber.test(passValue)) {
        strength++;
    }
    if (regList.hasLetter.test(passValue)) {
        strength++;
    }
    if (regList.hasSC.test(passValue)) {
        strength++;
    }

    if (regList.minlength6.test(passValue)) {
        return error("请输入六位以上的密码", pass, passError)
    }

    if (strength === 3) {
        success(pass, passError)
    } else if (strength === 2) {
        error("密码强度不足，建议升级密码", pass, passError)
    } else if (strength === 1) {
        error("密码强度严重不足，强烈建议升级密码", pass, passError)
    }
}
function loginSubmit() {
    for (var i = 0, ele; ele = validArr[i++];) {
        if (ele.className.split(" ").indexOf("success") === -1) {
            ele.focus();
            return;
        }
    }
    alert("登录成功!");
    location.href = "index.html";
}